﻿using DevExpress.XtraReports.UI;
using DevTeam;
using System;
using System.Data;
using System.Drawing;

namespace ConfExpert.Forms.Depo.Cikis
{
    public partial class ParcaCikisEtiketi : DevExpress.XtraReports.UI.XtraReport
    {
        DataSet ds;
        int EmptyLabelCount;
        public ParcaCikisEtiketi()
        {
            InitializeComponent();
        }
           

        public ParcaCikisEtiketi(string idxs, int bosetiket)
            : this()
        {
            EmptyLabelCount = bosetiket;
            ds = Session.DataManager.ExecuteDataSet("GetParcaCikisEtiket", idxs,2);

            BindDataTabletoCells();

            
        }

        void BindDataTabletoCells()
        {
            for (int i = 0; i < EmptyLabelCount; i++)
            {
                DataRow r= ds.Tables["Table"].NewRow();
                ds.Tables["Table"].Rows.InsertAt(r, 0);
            }            

            this.DataMember = "Table";
            this.DataSource = ds;

            tcMarka1.DataBindings.Add("Text", null, "Table.Marka");
            tcMarka2.DataBindings.Add("Text", null, "Table.Marka");

            tcSipNo1.DataBindings.Add("Text", null, "Table.Uretici");
            tcSipNo2.DataBindings.Add("Text", null, "Table.Uretici");

            //tcMArt1.DataBindings.Add("Text", null, "Table.MArticle");
            //tcMArt2.DataBindings.Add("Text", null, "Table.MArticle");

            tcStokKod1.DataBindings.Add("Text", null, "Table.StokKod");
            tcStokKod2.DataBindings.Add("Text", null, "Table.StokKod");

            tcKaynak1.DataBindings.Add("Text", null, "Table.KaynakTop");
            tcKaynak2.DataBindings.Add("Text", null, "Table.KaynakTop");

            tcPCE1.DataBindings.Add("Text", null, "Table.ParcaCikisEmriId");
            tcPCE2.DataBindings.Add("Text", null, "Table.ParcaCikisEmriId");

            tcIsteyen1.DataBindings.Add("Text", null, "Table.Isteyen");
            tcIsteyen2.DataBindings.Add("Text", null, "Table.Isteyen");          

            tcOrderRef1.DataBindings.Add("Text", null, "Table.OrderRef");
            tcOrderRef2.DataBindings.Add("Text", null, "Table.OrderRef");

            tcKalite1.DataBindings.Add("Text", null, "Table.Kalite");
            tcKalite2.DataBindings.Add("Text", null, "Table.Kalite");
        }
        
        private void Detail_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {            
            EmptyLabelCount--;
        }

        private static string MakeCriteria(string range)
        {
            string[] parts = range.Replace(" ", String.Empty).Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
            string str = String.Empty;
            foreach (string part in parts)
            {
                try
                {
                    int loc = part.IndexOf("-");
                    if (loc > -1)
                    {
                        int p1 = int.Parse("0" + part.Substring(0, loc).Replace("-", ""));
                        int p2 = int.Parse("0" + part.Substring(loc, part.Length - loc).Replace("-", ""));
                        if (p1 == 0 && p2 > 0)
                            str += " OR r.RollId <= " + p2.ToString();
                        else if (p1 > 0 && p2 == 0)
                            str += " OR r.RollId >= " + p1.ToString();
                        else
                            str += " OR r.RollId between " + p1.ToString() + " and " + p2.ToString();
                    }
                    else
                        str += " OR r.RollId = " + int.Parse(part).ToString();
                }
                catch
                {
                }
            }
            return str.Substring(4);
        }
        private void ReSize(object sender)
        {
            //Resize ı düzgün yapan tek fonksiyon !!!
            Graphics gr = Graphics.FromHwnd(IntPtr.Zero);
            gr.PageUnit = GraphicsUnit.Millimeter;
            int factor = 10;

            XRLabel label = (XRLabel)sender;

            float newSize = 8.25f;
            Font defaultFont = new System.Drawing.Font("Calibri", newSize);

            float stringWidth = gr.MeasureString(label.Text, defaultFont).Width;
            float labelWidth = label.HeightF;


            if (labelWidth < stringWidth * factor)
                newSize = defaultFont.Size * label.HeightF / stringWidth / factor;


            label.Font = new Font(defaultFont.FontFamily, newSize);
            gr.Dispose();
        }

        private void tcKalite2_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            ReSize(sender);
        }

        private void tcKalite1_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            ReSize(sender);
        }

        private void tcMarka1_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            ReSize(sender);
        }

        private void tcMarka2_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            ReSize(sender);
        }

        private void tcOrderRef1_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            ReSize(sender);
        }

        private void tcOrderRef2_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            ReSize(sender);
        }

        private void xrTable2_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            if (EmptyLabelCount > -1)
            {
                e.Cancel = true;
            }
            
        }

        private void tcKaynak1_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            ReSize(sender);
        }

        private void tcKaynak2_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            ReSize(sender);
        }
    }
}
